/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.widget.item.impl;

import com.genia.toolbox.web.gwt.basics.client.GwtHelper;
import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.basics.client.widget.richtext.RichTextWidget;
import com.genia.toolbox.web.gwt.form.client.CssNames;
import com.genia.toolbox.web.gwt.form.client.form.RichTextAreaItem;
import com.genia.toolbox.web.gwt.form.client.widget.item.ItemWidgetModifyListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.Widget;

/**
 * <code>SimpleItemWidget</code> that implements a <code>RichTextArea</code>.
 */
public class RichTextAreaItemWidget
    extends AbstractSimpleValuedItemWidget<RichTextAreaItem>
{

  /**
   * the <code>Panel</code> that will contain the other widgets.
   */
  private transient final HorizontalPanel hPanel = new HorizontalPanel();

  /**
   * the <code>RichTextArea</code> that will contain the text.
   */
  private transient final RichTextArea richTextArea = new RichTextArea();

  /**
   * the list of all the <code>Widget</code> that must be modify to update the
   * error message.
   */
  private transient final Widget[] widgetsToToggle = new Widget[] { richTextArea, hPanel };



  /**
   * constructor.
   * 
   * @param item
   *          the item to represent
   */
  public RichTextAreaItemWidget(final RichTextAreaItem item)
  {
    super(item);

    // get properties from RichTextAreaItem.
    final GwtI18nMessage labelKey = item.getLabelKey();
    final boolean enable = item.isEnabled();

    // get widgets.
    final RichTextWidget richTextWidget = new RichTextWidget(richTextArea);
    final Label label = new Label();

    GwtHelper.fillText(label, labelKey);

    hPanel.addStyleName(CssNames.RICH_TEXT_AREA_PANEL_CSS_STYLE);
    hPanel.addStyleName(item.getName());

    richTextArea.setEnabled(enable);

    hPanel.add(label);
    hPanel.add(richTextWidget);

    initWidget(hPanel);
  }



  /**
   * returns an array containing all the widget that the setErrorMessage must
   * modify.
   * 
   * @return an array containing all the widget that the setErrorMessage must
   *         modify
   */
  @Override
  protected Widget[] getWidgetsToToggle()
  {
    return widgetsToToggle;
  }



  /**
   * returns the value of a textBox.
   * 
   * @return the value of a textBox
   */
  @Override
  public String getValue()
  {
    return richTextArea.getHTML();
  }



  /**
   * add a listener that is called each time the value of this object change.
   * 
   * @param modifyListener
   *          the listener.
   */
  public void addModifyListener(final ItemWidgetModifyListener modifyListener)
  {
    richTextArea.addKeyboardListener(new KeyboardListenerAdapter()
    {

      @Override
      public void onKeyUp(final Widget widget, final char key, final int modifier)
      {
        modifyListener.onModify(RichTextAreaItemWidget.this);
      }

    });

  }



  /**
   * add a keyboard listener to the underlying TextBox.
   * 
   * @param keyboardListener
   *          the keyboard listener to add
   */
  public void addKeyboardListener(final KeyboardListener keyboardListener)
  {
    richTextArea.addKeyboardListener(keyboardListener);
  }



  /**
   * set the current value of this widget.
   * 
   * @param value
   *          the current value of this widget
   */
  @Override
  public void setValue(final String value)
  {
    richTextArea.setHTML(value);
  }
}
